package com.atguigu.controller;

import com.atguigu.client.SeckillFeignClient;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class WebSeckillController {
    @Autowired
    SeckillFeignClient seckillFeignClient;
    @GetMapping("/seckill-index.html")
    public String seckillIndex(Model model){
        model.addAttribute("list",seckillFeignClient.queryAllSeckillProduct().getData());
        return "seckill/index";
    }

//    //秒杀详情页
//    @GetMapping("/seckill-detail/{skuId}.html")
//    public String seckillDatail(@PathVariable Long skuId,Model model){
//        RetVal retVal = seckillFeignClient.getSecKillProductBySkuId(skuId);
//        model.addAttribute("item",retVal.getData());
//        return  "seckill/datail";
//    }

//2.秒杀商品详情页面编写
@GetMapping("/seckill-detail/{skuId}.html")
public String seckillDetail(@PathVariable Long skuId, Model model) {
    RetVal retVal = seckillFeignClient.getSecKillProductBySkuId(skuId);
    model.addAttribute("item", retVal.getData());
    return "seckill/detail";
}
    //获取请购吗成功后访问的页面
    @GetMapping("/seckill-queue.html")
    public String seckillQueue(Long skuId,Model model,String seckillCode){
        model.addAttribute("skuId", skuId);
        model.addAttribute("seckillCode",seckillCode);
        return  "seckill/queue";
    }
    //秒杀确认页面
    @GetMapping("/seckill-confirm.html")
    public String seckillConfirm(Model model){
        RetVal retVal = seckillFeignClient.seckillConfirm();
        if (retVal.isOk()){
            Map<String,Object> data = (Map<String,Object>)retVal.getData();
            model.addAllAttributes(data);
            return  "seckill/confirm";
        }else {
            model.addAttribute("message", retVal.getMessage());
        }
        return  "seckill/confirm";
    }
}


















